package it.unibo.util.information;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Information 
{
	private String name;
	private Map<String, String> attributes;
	private List<Information> children;
	
	public Information(String name)
	{
		if(name == null || name.isEmpty())
			throw new IllegalArgumentException("name must be not null and not empty");
		
		this.name = name;
		attributes = new HashMap<String, String>();
		children = new ArrayList<Information>();
	}
	
	public String getName()
	{
		return name;
	}
	
	public void addChildInformation(Information info)
	{
		children.add(info);
	}
	
	public void addChildInformations(List<Information> infos)
	{
		children.addAll(infos);
	}
	
	public void removeChildInformation(Information info)
	{
		children.remove(info);
	}
	
	public List<Information> getChildrenInformations()
	{
		return children;
	}
	
	public void addAttribute(String name, String value)
	{
		attributes.put(name, value);
	}
	
	public void removeAttribute(String name)
	{
		attributes.remove(name);
	}
	
	public void setAttribute(String name, String value)
	{
		if(attributes.containsKey(name))
			attributes.put(name, value);
	}
	
	public String getAttribute(String name)
	{
		return attributes.get(name);
	}
	
	public Map<String,String> getAttributes()
	{
		return attributes;
	}
	
	@Override
	public boolean equals(Object obj) 
	{
		if(obj == this)
			return true;
		
		if(obj == null || !(obj instanceof Information))
			return false;
		
		Information info = (Information)obj;
		
		// Se non hanno lo stesso nome non sono uguali
		if(!info.getName().equals(name))
			return false;
		
		// Se non hanno gli stessi attributi non sono uguali
		if(!info.getAttributes().equals(attributes))
			return false;
		
		// Se non hanno le stesse info figlie non sono uguali
		if(!info.getChildrenInformations().equals(children))
			return false;
		
		return true;
	}
}
